package org.jz.gae.redbook.spb.server.controller;

import java.util.List;

import org.jz.gae.redbook.spb.server.helper.EvidenceHelper;
import org.jz.gae.redbook.spb.server.helper.RedBookHelper;
import org.jz.gae.redbook.spb.shared.EvidenceEntityView;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

public class EvidenceControllerGoogle implements EvidenceController
{

	@Override
	public void loadEvidenceListForSpecies(
			Object _SpeciesKey,
			List<EvidenceEntityView> _EvidenceList)
	{
//	    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
//	    Query query = new Query(EvidenceHelper.ENTITY_NAME, redbookKey).addFilter(EvidenceHelper.EVIDENCE_SPECIES_KEY, FilterOperator.EQUAL, _SpeciesKey).addSort(EvidenceHelper.EVIDENCE_DATE, Query.SortDirection.DESCENDING);
//	    List<Entity> evidences = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
//	    if (evidences.isEmpty()) 
//	    {
//	    	_EvidenceList.clear();
//	    } 
//	    else 
//	    {
//	    	_EvidenceList.clear();
//	    	for (Entity evidence : evidences)
//	    	{
//	    		EvidenceEntityView newEvidence = new EvidenceEntityView();
//	    		EvidenceUtils.reflectEntityToView(evidence, newEvidence);
//	    		/**/
//	    		_EvidenceList.add(newEvidence);
//	    	}
//	    }		
	}
	
	@Override
	public void loadEvidenceListForLocation(
			Object _LocationKey,
			List<EvidenceEntityView> _EvidenceList)
	{
	    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
	    Query query = new Query(EvidenceHelper.ENTITY_NAME, redbookKey).addFilter(EvidenceHelper.EVIDENCE_LOCATION_KEY, FilterOperator.EQUAL, _LocationKey).addSort(EvidenceHelper.EVIDENCE_DATE, Query.SortDirection.DESCENDING);
	    List<Entity> evidences = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
	    if (evidences.isEmpty()) 
	    {
	    	_EvidenceList.clear();
	    } 
	    else 
	    {
	    	_EvidenceList.clear();
	    	for (Entity evidence : evidences)
	    	{
	    		EvidenceEntityView newEvidence = new EvidenceEntityView();
	    		EvidenceUtils.reflectEntityToView(evidence, newEvidence);
	    		/**/
	    		_EvidenceList.add(newEvidence);
	    	}
	    }		
	}

	@Override
	public void updateEvidence(
			EvidenceEntityView _EvidenceToEdit) throws Exception
	{
		System.err.println("extraction key : " + _EvidenceToEdit.getEvidenceKey());
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity evidence = datastore.get(KeyFactory.stringToKey(_EvidenceToEdit.getEvidenceKey()));
		System.err.println("extracted");
		EvidenceUtils.reflectViewToEntity(_EvidenceToEdit, evidence);
        datastore.put(evidence);
		System.err.println("updated");
	}

	@Override
	public void deleteEvidence(
			EvidenceEntityView _EvidenceToDelete)
	{
		System.err.println("deletion key : " + _EvidenceToDelete.getEvidenceKey());
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.delete(KeyFactory.stringToKey(_EvidenceToDelete.getEvidenceKey()));
		System.err.println("deleted");
	}

	@Override
	public void createEvidence(
			EvidenceEntityView _EvidenceToAppend)
	{
	    Key redbookKey = KeyFactory.createKey(RedBookHelper.RED_BOOK_SIGN, RedBookHelper.RED_BOOK_NAME);
		Entity newEvidence = new Entity(EvidenceHelper.ENTITY_NAME, redbookKey);
		EvidenceUtils.reflectViewToEntity(_EvidenceToAppend, newEvidence);
        /**/
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(newEvidence);
		System.err.println("new key : " + newEvidence.getKey());
		_EvidenceToAppend.setEvidenceKey(KeyFactory.keyToString(newEvidence.getKey()));
	}

}
